package com.ledim.ledimview;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.ledim.adapter.EpisodesAdapter;
import com.ledim.adapter.FiguresAdapter;
import com.ledim.bean.LedimAlbumBean;
import com.ledim.bean.LedimEpisodeBean;
import com.ledim.itf.LedimAlbusFragmentInterface;
import com.ledim.utils.ConstantUtil;
import com.ledim.utils.DensityUtil;
import com.ledim.widget.horizontallist.widget.AdapterView;
import com.ledim.widget.horizontallist.widget.HListView;
import com.letv.android.client.activity.AlbumPlayActivity;
import com.letv.android.young.client.R;
import java.util.ArrayList;
import java.util.Iterator;

public class VideoEpisodesView extends LinearLayout implements View.OnClickListener {

    private static final int VARITYSHOW = 1;
    private static final int POSITIVE = 180001;
    private static final int TV = 181031;
    private View mEpisodesLayout;
    private TextView mType;
    private TextView mCurrent;
    private HListView mListView;
    private FiguresAdapter mFiguresAdapter;
    private EpisodesAdapter mImagesAdapter;
    private LedimAlbumBean album;
    private ArrayList<LedimEpisodeBean> episodes;
    private Context mContext;
    private int nowEpisodes = 0;
    private int mPlayVid = 0;
    public LedimEpisodeBean mCurrentEpisode;
    private LedimAlbusFragmentInterface mEpisodeInterface;

    private AlbumPlayActivity mActivity;

    public VideoEpisodesView(Context context) {
        super(context);
        mContext = context;
    }

    public VideoEpisodesView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public VideoEpisodesView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
    }

    public void setActivity(AlbumPlayActivity activity) {
        this.mActivity = activity;
    }

    //绑定数据
    public void bindData(int mPlayVid, ArrayList<LedimEpisodeBean> episodes, LedimAlbumBean album,
                         LedimAlbusFragmentInterface episodeInterface) {
        this.episodes = episodes;
        this.album = album;
        this.mPlayVid = mPlayVid;
        mEpisodeInterface = episodeInterface;
        init();
    }

    //电视剧、动漫类显示10个剧集
    private void getEpisodesLists() {
        //显示剧集
        WindowManager wm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        int mWidth = wm.getDefaultDisplay().getWidth();
        int mHeight = wm.getDefaultDisplay().getHeight();
        if (mWidth > mHeight) {
            mWidth = mHeight;
        }
        mWidth = (int) ((mWidth - DensityUtil.dip2px(mContext, 48)) / 5.5);
        android.view.ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = mWidth + 2;
        mListView.setLayoutParams(params);

        if (mFiguresAdapter == null) {
            mFiguresAdapter = new FiguresAdapter(mContext, episodes);
            mListView.setAdapter(mFiguresAdapter);
        } else {
            mFiguresAdapter.setDatas(episodes);
        }
        //进入播放页面选中的
        for (int i = 0; i < episodes.size(); i++) {
            if (episodes.get(i).id == mPlayVid) {
                mFiguresAdapter.setSelectItem(i);
                //mEpisodeInterface.setDescription(episodes.get(i));
                mCurrentEpisode = episodes.get(i);
                mFiguresAdapter.notifyDataSetChanged();
                mListView.setSelection(i);
                mEpisodeInterface.setDescription(mCurrentEpisode);
            }
        }
    }

    /**
     * 刷新剧集界面
     */
    public void reFreshEpisode(int vid, ArrayList<LedimEpisodeBean> changeEpisodes) {
        if (mFiguresAdapter != null) {
            for (int i = 0; i < changeEpisodes.size(); i++) {
                if (changeEpisodes.get(i).id == vid) {
                    mFiguresAdapter.setSelectItem(i);
                    mFiguresAdapter.notifyDataSetChanged();
                    mPlayVid = vid;
                    mCurrentEpisode = changeEpisodes.get(i);
                    mListView.setSelection(i);
                    mEpisodeInterface.setDescription(mCurrentEpisode);
                }
            }
        }

        if (mImagesAdapter != null) {
            for (int i = 0; i < changeEpisodes.size(); i++) {
                if (changeEpisodes.get(i).id == vid) {
                    mImagesAdapter.setSelectItem(i);
                    mImagesAdapter.notifyDataSetChanged();
                    mPlayVid = vid;
                    mCurrentEpisode = changeEpisodes.get(i);
                    mListView.setSelection(i);
                }
            }
        }
        if (mCurrentEpisode != null) {
            mEpisodeInterface.setDescription(mCurrentEpisode);
        }
    }

    /**
     * 初始化界面
     */
    private void init() {
        mEpisodesLayout = findViewById(R.id.video_episodes_layout);
        mType = (TextView) findViewById(R.id.video_type);
        mCurrent = (TextView) findViewById(R.id.video_current_episode);
        mListView = (HListView) findViewById(R.id.horizontal_listview);

        mEpisodesLayout.setOnClickListener(this);
        mCurrent.setOnClickListener(this);
        // ListView的单选模式
        mListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        getEpisodesShow();
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mImagesAdapter != null) {
                    mImagesAdapter.setSelectItem(position);
                    mCurrentEpisode = episodes.get(position);
                    mImagesAdapter.notifyDataSetChanged();
                    mActivity.setVid(episodes.get(position).id, 0);
                    mPlayVid = episodes.get(position).id;
                }
                if (mFiguresAdapter != null) {
                    mFiguresAdapter.setSelectItem(position);
                    mFiguresAdapter.notifyDataSetChanged();
                    mCurrentEpisode = episodes.get(position);
                    mActivity.setVid(episodes.get(position).id, 0);
                    mPlayVid = episodes.get(position).id;
                }
                if (mCurrentEpisode != null) {
                    mEpisodeInterface.addPlayListEpisode(mCurrentEpisode);
                    mEpisodeInterface.setDescription(mCurrentEpisode);
                }
            }
        });

    }

    //综艺类类显示10个图片
    private void getEpisodesCards() {
        nowEpisodes = -1;
        //显示视频
        android.view.ViewGroup.LayoutParams params = mListView.getLayoutParams();
        params.height = DensityUtil.dip2px(mContext, 115);
        if (mImagesAdapter == null) {
            mImagesAdapter = new EpisodesAdapter(mContext, episodes);
            mListView.setAdapter(mImagesAdapter);
        } else {
            mImagesAdapter.setDatas(episodes);
            mImagesAdapter.notifyDataSetChanged();
        }
        //进入播放页面选中的
        for (int i = 0; i < episodes.size(); i++) {
            if (episodes.get(i).id == mPlayVid) {
                mImagesAdapter.setSelectItem(i);
                mCurrentEpisode = episodes.get(i);
                mImagesAdapter.notifyDataSetChanged();
                mListView.setSelection(i);
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.video_current_episode:
            case R.id.video_episodes_layout:
                mEpisodeInterface.showAlbus(nowEpisodes, mPlayVid);
                break;
        }
    }

    //显示剧集信息
    private void getEpisodesShow() {
        switch (album.varietyShow) {
            case VARITYSHOW://栏目
                mType.setText("专辑列表");
                if (album.episode != 0) {
                    mCurrent.setText(album.episode + "");
                } else {
                    mCurrent.setText("更多");
                }
                getEpisodesCards();
                break;
            default: // 0--非栏目
                if (album.albumType != null && !album.albumType.equals("null")) {
                    Iterator<Integer> iterator = album.albumType.keySet().iterator();
                    int albumType = -1;
                    while (iterator.hasNext()) {
                        albumType = iterator.next();
                    }
                    switch (albumType) {
                        case POSITIVE://正片
                        case TV://TV版
                            if (album.getCategory() == ConstantUtil.ChannelType.film) {//电影
                                mType.setText("正片");
                                if (album.episode != 0) {
                                    mCurrent.setText(album.episode + "");
                                } else {
                                    mCurrent.setText("更多");
                                }
                                getEpisodesCards();
                            } else if (album.getCategory() == ConstantUtil.ChannelType.tv ||
                                    album.getCategory() == ConstantUtil.ChannelType.cartoon) {//电视剧
                                mType.setText("剧集");
                                if (album.isEnd == 1) {//完结
                                    mCurrent.setText(album.episode + "集全");
                                    if (album.episode != 0) {
                                        nowEpisodes = album.episode;
                                    }
                                } else {
                                    mCurrent.setText("更新至" + album.nowEpisodes + "集");
                                    if (album.nowEpisodes != 0) {
                                        nowEpisodes = album.nowEpisodes;
                                    }
                                }
                                getEpisodesLists();

                            } else if (album.getCategory() ==
                                    ConstantUtil.ChannelType.variety) {//综艺
                                mType.setText("往期列表");
                                String episode = album.nowIssue + "";
                                if (episode != null && episode.length() == 8) {
                                    if (album.isEnd == 1) {//完结
                                        mCurrent.setText(album.episode + "期全");
                                    } else {
                                        String episode1 = episode.substring(0, 4);
                                        String episode2 = episode.substring(4, 6);
                                        String episode3 = episode.substring(6, episode.length());
                                        mCurrent.setText(
                                                "更新至" + episode1 + "-" + episode2 + "-" + episode3 +
                                                        "期");
                                    }
                                } else {
                                    if (album.isEnd == 1) {//完结
                                        mCurrent.setText(episode + "期全");
                                    } else {
                                        mCurrent.setText("更新至" + episode + "期");
                                    }
                                }
                                getEpisodesCards();
                            } else {//纪录片等其它
                                mType.setText("相关视频");
                                if (album.episode != 0) {
                                    mCurrent.setText(album.episode + "");
                                } else {
                                    mCurrent.setText("更多");
                                }
                                getEpisodesCards();
                            }
                            break;
                        default:
                            mType.setText("相关视频");
                            if (album.episode != 0) {
                                mCurrent.setText(album.episode + "");
                            } else {
                                mCurrent.setText("更多");
                            }
                            getEpisodesCards();
                            break;
                    }
                } else {
                    //null--娱乐
                    mType.setText("相关视频");
                    if (album.episode != 0) {
                        mCurrent.setText(album.episode + "");
                    } else {
                        mCurrent.setText("更多");
                    }
                    getEpisodesCards();
                }
                break;
        }
    }

}
